#define _CRT_SECURE_NO_WARNINGS 1

#include "SeqList.h"

void SeqListInit(SL* ps)
{
    ps->a = NULL;
    ps->size = 0;
    ps->capacity = 0;
}

void SeqListPushBack(SL* ps, SQDataType x)
{
    if (ps->size == ps->capacity)
    {
        int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
        SQDataType* tmp = (SQDataType*)realloc(ps->a, newcapacity * sizeof(SQDataType));
        if (tmp == NULL)
        {
            perror("realloc fail");
            return;
        }
        ps->a = tmp;
        ps->capacity = newcapacity;
    }
    ps->a[ps->size] = x;
    ps->size++;
}

void SeqListPushFront(SL* ps, SQDataType x)
{
    if (ps->size == ps->capacity)
    {
        int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
        SQDataType* tmp = (SQDataType*)realloc(ps->a, newcapacity * sizeof(SQDataType));
        if (tmp == NULL)
        {
            perror("realloc fail");
            return;
        }
        ps->a = tmp;
        ps->capacity = newcapacity;
    }

    for (int i = ps->size; i > 0; i--)
    {
        ps->a[i] = ps->a[i - 1];
    }
    ps->a[0] = x;
    ps->size++;
}

void SeqListPopBack(SL* ps)
{
    if (ps->size == 0)
    {
        printf("SeqList is empty\n");
        return;
    }
    ps->size--;
}

void SeqListPopFront(SL* ps)
{
    if (ps->size == 0)
    {
        printf("SeqList is empty\n");
        return;
    }
    for (int i = 0; i < ps->size - 1; i++)
    {
        ps->a[i] = ps->a[i + 1];
    }
    ps->size--;
}

void SeqListPrint(SL* ps)
{
    for (int i = 0; i < ps->size; i++)
    {
        printf("%d ", ps->a[i]);
    }
    printf("\n");
}

void SeqListDestroy(SL* ps)
{
    if (ps->a != NULL)
    {
        free(ps->a);
        ps->a = NULL;
    }
    ps->size = 0;
    ps->capacity = 0;
}